﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;

namespace BLL.Models
{
    public class MAdModels
    {
        string[] str_files = {"ID","Adname","Adurl","Adpic","Flag","Adtitle" };
        /// <summary>
        /// 修改广告
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateMad(MODEL.MAD model) {

            bool ret = false;
            string str_sql = "update   [tb_mc_ad] set adtitle='"+model.Adtitle+"' ,adurl='"+model.Adurl+"',adpic='"+model.Adpic+"' where id=" +model.Id.ToString();
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 获取全部广告
        /// </summary>
        /// <returns></returns>
        public string AllMad(string start,string limit,string fiter) {

            string ret = string.Empty;
            string str_sql = "select top " + limit + " * from [tb_mc_ad] where id not in( select top  " + start + " id from [tb_mc_ad] order by id desc ) order by id desc";
            if (start == "0")
            {

                str_sql = " select top " + limit + " * from [tb_mc_ad] order by id desc ";
            }
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();
            string tcount = "0";
            try
            {
                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
                tcount = db.ExecuteScalar(COMM.PucConfig.ConnectionStringA, CommandType.Text, "select count(*) from [tb_mc_ad] ").ToString();
            }
            catch (Exception ex) { }

            BLL.JSONHelper jsons = new BLL.JSONHelper();



            foreach (DataRow dr in dt.Rows)
            {

                foreach (string s in str_files)
                {

                    jsons.AddItem(s, dr[s].ToString());
                }
                jsons.ItemOk();

            }


            jsons.totlalCount = Convert.ToInt32(tcount);
            ret = jsons.ToString();
            return ret;
        }
        /// <summary>
        /// 获取一条广告
        /// </summary>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public MODEL.MAD GetmadByFiter(string fiter) {

            MODEL.MAD model = new MODEL.MAD();
            return model;
        }
        /// <summary>
        /// 获得一条广告信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetMadByid(string id) {

            string ret = string.Empty;
            string str_sql = "select * from [tb_mc_ad] where id=" + id;
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();
            try
            {
                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }
            BLL.FormJSONHelper jsons = new BLL.FormJSONHelper();
            jsons.success = true;


            foreach (DataRow dr in dt.Rows)
            {

                foreach (string s in str_files)
                {

                    jsons.AddItem(s, dr[s].ToString());
                }
                jsons.ItemOk();

            }



            ret = jsons.ToString();
            return ret;
        }
        /// <summary>
        /// 根据指定条件获取广告信息
        /// </summary>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public DataTable GetAdByFiter(string fiter) {

            string str_sql = "select * from [tb_mc_ad] where " + fiter;
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();
            try
            {
                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }
            return dt;
        }
        /// <summary>
        /// 取得首页广告中间部分
        /// </summary>
        /// <returns></returns>
        public string GetIndexMidAd() {

            string ret = string.Empty;
            DataTable dt = new DataTable();
            StringBuilder str_ad = new StringBuilder();
            dt=GetAdByFiter(" id in(1,2,3,4,5) order by id  ");
           foreach(DataRow dr in dt.Rows){
           
               str_ad.Append("<LI class=cur><A  href='");
               str_ad.Append(dr["adurl"].ToString());
               str_ad.Append("'   target=_blank><IMG alt='");
               str_ad.Append(dr["adtitle"].ToString());
               str_ad.Append("'   src='");
               str_ad.Append(dr["adpic"].ToString());
               str_ad.Append("'></A>");
           }

              str_ad.Append("</LI>");
              ret = str_ad.ToString();
            return ret;
        }
        /// <summary>
        /// 获得首页广告文字部分
        /// </summary>
        /// <returns></returns>
        public string GetIndexMidAdtxt()
        {

            string ret = string.Empty;
            DataTable dt = new DataTable();
            StringBuilder str_ad = new StringBuilder();
            dt = GetAdByFiter(" id in(1,2,3,4,5) order by id  ");
            foreach (DataRow dr in dt.Rows)
            {

                str_ad.Append("<LI class=cur><A  href='");
                str_ad.Append(dr["adurl"].ToString());
                str_ad.Append("'   target=_blank>");
                str_ad.Append(dr["adtitle"].ToString());
                str_ad.Append("</A>");
            }

            str_ad.Append("</LI>");
            ret = str_ad.ToString();
            return ret;
        }
        /// <summary>
        /// 返回首页所需要的modellist
        /// </summary>
        /// <returns></returns>
        public List<MODEL.MAD> GetIndexAD() {

            List<MODEL.MAD> models = new List<MODEL.MAD>();
            string str_sql = "select * from [tb_mc_ad] where id<15";
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();
            try
            {
                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }
            MODEL.MAD model = new MODEL.MAD();
            foreach(DataRow dr in dt.Rows){
                MODEL.MAD modelt=new MODEL.MAD();
                BuildModeFromDr(dr, ref modelt);
                models.Add(modelt);
            }
            return models;
        }
        /// <summary>
        /// 通过datarow构造model
        /// </summary>
        /// <param name="dr"></param>
        /// <param name="model"></param>
        public void BuildModeFromDr(DataRow dr,ref MODEL.MAD model) {

            model.Id =Convert.IsDBNull(dr["id"])==true?0:Convert.ToInt32( dr["id"].ToString());
            model.Adname = Convert.IsDBNull(dr["adname"]) == true ? "" : dr["adname"].ToString();
            model.Adpic = Convert.IsDBNull(dr["adpic"]) == true ? "" : dr["adpic"].ToString();
            model.Adtitle = Convert.IsDBNull(dr["adtitle"]) == true ? "" : dr["adtitle"].ToString();
            model.Adurl = Convert.IsDBNull(dr["adurl"]) == true ? "" : dr["adurl"].ToString();
            model.Flag = Convert.IsDBNull(dr["flag"]) == true ? 0 :Convert.ToInt32( dr["flag"].ToString());

        }
        /// <summary>
        /// 取得商品列表页面广告
        /// </summary>
        /// <returns></returns>
        public MODEL.MAD GetListAd() {

            MODEL.MAD model = new MODEL.MAD();
            DataTable dt = new DataTable();
            dt = GetAdByFiter(" id= 15");
            BuildModeFromDr(dt.Rows[0],ref model);
            return model;
        }
        /// <summary>
        /// 按id取得广告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MODEL.MAD GetAdById(string id) {
            MODEL.MAD model = new MODEL.MAD();
            DataTable dt = new DataTable();
            dt = GetAdByFiter(" id= "+id);
            BuildModeFromDr(dt.Rows[0], ref model);
            return model;
        }
        /// <summary>
        /// 按分类取得广告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public MODEL.MAD GetAdByClassId(string id)
        {
            MODEL.MAD model = new MODEL.MAD();
            DataTable dt = new DataTable();
            dt = GetAdByFiter(" classid= " + id);
            BuildModeFromDr(dt.Rows[0], ref model);
            return model;
        }
        /// <summary>
        /// 按id从列表里取得指定广告
        /// </summary>
        /// <param name="models"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public MODEL.MAD GetAdByClassId(List<MODEL.MAD> models, string id) {

            MODEL.MAD ret = new MODEL.MAD();
            foreach(MODEL.MAD model in models){
            
                if(model.Id.ToString()==id){

                    ret = model;
                }
            }
            return ret;
        }
    }
}
